﻿using System;
using System.Collections.Generic;
using System.Text;

namespace GeoFly
{
    public class PETInFAOPM : PET
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tav"></param>
        /// <param name="elev"></param>
        /// <param name="tmx"></param>
        /// <param name="tmn"></param>
        /// <param name="yr"></param>
        /// <param name="dn"></param>
        /// <param name="lat">在默认情况下为33</param>
        public PETInFAOPM(double tav, double elev, double tmx, double tmn, int yr, int dn, double lat)
            : base(yr, dn, lat)
        {
            dTav = tav;
            dElev = elev;
            dTmx = tmx;
            dTmn = tmn;
            dTmxk = dTmx + 273.15;
            dTmnk = dTmn + 273.15;
            pwatvap = new WaterVaporFAOPM(dTav, dElev, dTmx, dTmn);
        }
        /// <summary>
        /// 总长波辐射
        /// </summary>
        /// <param name="slrp">日照百分率</param>
        /// <param name="avp">实际水汽压</param>
        /// <returns></returns>
        public override double NetLongWaveRadAvp(double slrp, double avp)
        {
            if (avp <= 0) avp = 0.5;
            dNetLong = -2.45 * Math.Pow(10, -9) * (0.1 + 0.9 * slrp) * (0.34 - 0.14 * Math.Sqrt(avp)) * (Math.Pow(dTmxk, 4) + Math.Pow(dTmnk, 4));
            return dNetLong;
        }
        public override double NetLongWaveRadRHmd(double slrp, double rhmd)
        {
            double davp = 0;
            davp = pwatvap.ActVapPressureByTMxMn(rhmd);
            return NetLongWaveRadAvp(slrp, davp);
        }
        public double NetLongWaveRadiationAvp(double slrg, double avp)
        {
            //if (avp <= 0) avp = 0.5;
            //dNetLong = -2.45 * Math.Pow(10, -9) * (0.1 + 0.9 * slrg / pslr.RealSolarRadMax(0.85)) * (0.34 - 0.14 * Math.Sqrt(avp)) * (Math.Pow(dTmxk, 4) + Math.Pow(dTmnk, 4));
            //return dNetLong;
            throw new Exception("");
        }
        public double NetLongWaveRadiationRHmd(double slrg, double rhmd)
        {
            double davp = 0;
            davp = pwatvap.ActVapPressureByTMxMn(rhmd);
            return NetLongWaveRadAvp(slrg, davp);
        }

        public double PETByRAVP(int dn, double wspeed, double avp, double G)
        {
            double dPet = 0;
            double Hnet = NetRadiation();
            double dlta = pwatvap.TmpVapCurveSlp();
            double svp = pwatvap.SatuVapPressure();
            //	double psy = pwatvap.PsychroConst(dn);
            double psy = pwatvap.PsychroConst();
            dEnergy = (0.408 * dlta * (Hnet - G)) / (dlta + psy * (1 + 0.34 * wspeed));
            dAero = (psy * 900 * wspeed * (svp - avp) / (dTav + 273)) / (dlta + psy * (1 + 0.34 * wspeed));
            dPet = dEnergy + dAero;
            return dPet;

        }
        /// <summary>
        /// G在默认情
        /// </summary>
        /// <param name="dn"></param>
        /// <param name="wspeed"></param>
        /// <param name="rhmd"></param>
        /// <param name="G">默认为0</param>
        /// <returns></returns>
        public double PETInPMByRHmd(int dn, double wspeed, double rhmd, double G)
        {
            double avp = pwatvap.ActVapPressureByTMxMn(rhmd);
            return PETByRAVP(dn, wspeed, avp, G);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="wspeed"></param>
        /// <param name="avp"></param>
        /// <param name="G">默认为0</param>
        /// <returns></returns>
        public double PETByRAVP(double wspeed, double avp, double G)
        {
            double dPet = 0;
            double Hnet = NetRadiation();
            double dlta = pwatvap.TmpVapCurveSlp();
            double svp = pwatvap.SatuVapPressure();
            //	double psy = pwatvap.PsychroConst(dn);
            double psy = pwatvap.PsychroConst();
            dEnergy = (0.408 * dlta * (Hnet - G)) / (dlta + psy * (1 + 0.34 * wspeed));
            dAero = (psy * 900 * wspeed * (svp - avp) / (dTav + 273)) / (dlta + psy * (1 + 0.34 * wspeed));
            dPet = dEnergy + dAero;
            return dPet;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="wspeed"></param>
        /// <param name="rhmd"></param>
        /// <param name="G">默认为0</param>
        /// <returns></returns>
        public double PETInPMByRHmd(double wspeed, double rhmd, double G)
        {
            double avp = pwatvap.ActVapPressureByTMxMn(rhmd);
            return PETByRAVP(wspeed, avp, G);
        }

        public double NetRadiation()
        {
            return dNetShort + dNetLong;
        }


        WaterVaporFAOPM pwatvap;
    }
}